package com.scalar.chiptrack.operations.dataentry.lotcorrection.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.forms.LotCorrectionForm;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.LotCorrectionHelper;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Ganesh
 * Date: Nov 28, 2006
 * Time: 12:30:36 PM
 */
public class LotCorrectionLoadProcessTypesCommand extends ActionCommand 
{
    private static final String ACTION_NAME = "/lotCorrection.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {


        LotCorrectionForm lotCorrectionForm = (LotCorrectionForm) form;
        LotCorrectionHelper lotCorrectionHelper = LotCorrectionHelper.getInstance();
        String action = request.getParameter("action");

        HashMap errors = (HashMap)request.getAttribute(ERROR_MESSAGES);
        if ( errors == null )
        {
            errors = new HashMap();
            request.setAttribute(ERROR_MESSAGES, errors );
        }

        HashMap info = (HashMap)request.getAttribute(INFO_MESSAGES);
        if ( info == null )
        {
            info = new HashMap();
            request.setAttribute(INFO_MESSAGES, info );
        }

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        try
        {
            if( action == null)
            {
                return mapping.findForward("lotCorrectionType");
            }
            ArrayList processList;

            lotCorrectionForm.setProcessType("0");
            lotCorrectionForm.setProcessStepId("0");
            lotCorrectionForm.setLocationId("0");
            lotCorrectionForm.setLotId("0");
            processList = lotCorrectionHelper.getProcessList();

            request.setAttribute("processList", processList);
            request.setAttribute("action", action);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("lotCorrection");
    }
}
